package classes.controlers {
	import classes.managers.Game_Manager;
	import classes.sounds.SoundCollection;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.utils.Timer;
	/**
	 * ...
	 * @author Kihoonee
	 */
	public class Timer_Controler {
		private var gm:Game_Manager;
		private var txt:TextField;
		
		private var _sec:int;
		private var _milSec:int = 0;
		private var milSecTimer:Timer = new Timer( 1 );
		private var secTimer:Timer = new Timer( 1000 );
		
		public function Timer_Controler( _gm:Game_Manager, _txt:TextField ) {
			gm = _gm;
			txt = _txt;
			
			secTimer.addEventListener(TimerEvent.TIMER, secTIMER );
			milSecTimer.addEventListener(TimerEvent.TIMER, milSecTIMER );
		}
		
		public function init():void {
			sec = 31;
			milSec = 0;
			txt.text = "00";
			secTimer.reset();
			milSecTimer.reset();
		}
		
		private function secTIMER(e:TimerEvent):void {
			SoundCollection.clockSound();
			
			--sec;
			txt.text = String( sec );
			
			if ( sec == 50 ) {
				gm.remainTime( sec );
			}else if ( sec == 40 ) {
				gm.remainTime( sec );
			}else if ( sec == 30 ) {
				gm.remainTime( sec );
			}else if ( sec == 20 ) {
				gm.remainTime( sec );
			}else if ( sec == 10 ) {
				gm.remainTime( sec );
			}else if ( sec == 5 ) {
				gm.remainTime( sec );
			}else if ( sec === 0 ) {
				timeOver();
			}
		}
		
		private function milSecTIMER(e:TimerEvent):void {
			milSec++;
			if ( milSec === 100 ) {
				milSec = 0;
			}
		}
		
		private function timeOver():void {
			gm.timeOver();
			secTimer.stop();
			milSecTimer.stop();
		}
		
		public function start():void {
			secTimer.start();
			milSecTimer.start();
		}
		
		public function stop():void {
			secTimer.stop();
			milSecTimer.stop();
		}
		
		public function get sec():int { return _sec; }
		public function set sec(value:int):void { _sec = value; }
		
		public function get milSec():int { return _milSec; }
		public function set milSec(value:int):void { _milSec = value;
		}
	}
}